//
//  LuckyMoneyViewController.m
//  meiyinxiang
//
//  Created by apple on 2017/10/17.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "LuckyMoneyViewController.h"

#import "PostLuckyMoneyNoticeHeaderSectionView.h"
//@interface NIMKitMediaPickerController : TZImagePickerController
//
//@end
//@implementation NIMKitMediaPickerController
//
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    [UIApplication sharedApplication].statusBarStyle =  UIStatusBarStyleDefault;
//}
//
//@end
@interface LuckyMoneyViewController ()<
UITableViewDelegate,
UITableViewDataSource>
{
    NSString *textContent;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;


- (IBAction)backBtn:(id)sender;
- (IBAction)sendLuckyBtn:(id)sender;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeightLayoutCons;//导航栏高度

@end

@implementation LuckyMoneyViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _viewHeightLayoutCons.constant = SafeAreaTopHeight;
    //添加通知
    [self addNotification];
    
    
    //[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:PostLukyImgNotificationName object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:PostChangeLocationRangeName object:nil];
    
}
- (void)awakeFromNib{
    [super awakeFromNib];
    self.navigationController.navigationBarHidden = YES;
    if (_compViewModel == nil) {
        _compViewModel = [[ComeposeLuckyViewModel alloc] init];
    }
    if (_lukyMoneyShowView == nil) {
        _lukyMoneyShowView = [AddLukyMoneyShowView loadNibWithLukyMoneyShowView];
        _lukyMoneyShowView.hidden = YES;
        [self.view addSubview:_lukyMoneyShowView];
        [self.view bringSubviewToFront:_lukyMoneyShowView];
    }
    if (_mapVC == nil) {
        _mapVC = [MapLocateViewController loadNibWithMapLocationVC];
    }
    if (_rangeModel == nil) {
        _rangeModel = [[LocationRangeModel alloc] init];
        _rangeModel.lati = [NSString stringWithFormat:@"%f", ApplicationDelegate.locationcoor.latitude];
        _rangeModel.lon = [NSString stringWithFormat:@"%f", ApplicationDelegate.locationcoor.longitude];
        _rangeModel.locationRangeRadius = 3000;
    }
    
    [_compViewModel getResource];
    AdjustsScrollViewInsetNever(self, self.tableView);
    [self configTableView];
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - Actions
- (void)selectPhonePhotoBtn:(id)sender {
    
}

- (IBAction)backBtn:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"MISS SUCCESS ");
    }];
}

- (IBAction)sendLuckyBtn:(id)sender {
    [_textCell.growTextView resignFirstResponder];
    [_textCell uploadImgs];
    if (self.sendLuckyBtn.currentTitleColor == [UIColor whiteColor]) {
        _lukyMoneyShowView.mSuperViewController = self;
        [_lukyMoneyShowView addLuckyMoneyAnnamation];
    }else{
        ShowSVProgressErrorMessage(@"亲，再多加点文字描述吧～～");
        // [SVProgressHUD showErrorWithStatus:@"亲，再多加点文字描述吧～～"];
    }
    
}
#pragma mark -TableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else if (section == 2){
        ComposeMoneyModel *compModel =  _compViewModel.modelArray[0];
        return compModel.isSelected ? compModel.sectionCategory.count : 0;
    }else if (section == 3){
        ComposeMoneyModel *compModel =  _compViewModel.modelArray[1];
        return compModel.isSelected ? compModel.sectionCategory.count : 0;
    }else if (section == 4){
        ComposeMoneyModel *compModel =  _compViewModel.modelArray[2];
        return compModel.isSelected ? compModel.sectionCategory.count : 0;
    }else{
        return 0;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return _contentViewHeight;
    }else if (indexPath.section == 1){
        return 40;
    }else{
        return 49;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else if (section == 1){
        return 15;
    }else{
        return 48;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return 15;
    }else{
        return 0;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        _textCell = [tableView dequeueReusableCellWithIdentifier:MYXLuckyTextReuseId forIndexPath:indexPath];
        if (_textCell == nil) {
            _textCell = [MYXLuckyTxteViewCell loadNibWithTxteViewCell];
        }
        _textCell.mSuperViewController = self;
        
        return _textCell;
    }else if (indexPath.section == 1){
        MYXSelectPhoteAndVideoTableViewCell *photoCell = [tableView dequeueReusableCellWithIdentifier:MYXSelectPhoteCellReuseId forIndexPath:indexPath];
        if (photoCell == nil) {
            photoCell =  [MYXSelectPhoteAndVideoTableViewCell loadNibWithCell];
            
        }
        photoCell.myController = self;
        return photoCell;
    }else{
        MYXLuckyCategoryCell *cateCell = [tableView dequeueReusableCellWithIdentifier:MYXLuckyCategoryCellReuseId forIndexPath:indexPath];
        if (cateCell == nil) {
            cateCell = [MYXLuckyCategoryCell loadNibWithMYXLuckyCell];
        }
        ComposeMoneyModel *model = _compViewModel.modelArray[indexPath.section - 2];
        ComposeCateModel *cateModel = model.sectionCategory[indexPath.row];
        cateCell.model = cateModel;
        return cateCell;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return nil;
    }
    if (section == 5) {
        PostLuckyMoneyNoticeHeaderSectionView *footerview = [PostLuckyMoneyNoticeHeaderSectionView loadNibWithPostView];
        return footerview;
    }
    MXYLukyMoneySectionView *sectionView = [MXYLukyMoneySectionView loadNibWithSectionView];
    ComposeMoneyModel *model = _compViewModel.modelArray[section - 2];
    sectionView.model = model;
    sectionView.section = section;
    [sectionView addTarget:self action:@selector(selectSectionView:) forControlEvents:UIControlEventTouchDown];
    return sectionView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section > 1) {
        ComposeMoneyModel *model = _compViewModel.modelArray[indexPath.section - 2];
        for ( ComposeCateModel *cateModel in model.sectionCategory) {
            if (cateModel == model.sectionCategory[indexPath.row]) {
                cateModel.selected = YES;
                model.sectionCateTitle = cateModel.cateString;
            }else{
                cateModel.selected = NO;
            }
        }
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
        [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
    }
}
#pragma mark - PrivateMethod

- (void)selectSectionView:(MXYLukyMoneySectionView *)sectioview{
    //sectioview.isSelected = !sectioview.isSelected;
    if (sectioview.section != 4) {
        ComposeMoneyModel *compModel =  _compViewModel.modelArray[sectioview.section - 2];
        compModel.isSelected = !compModel.isSelected;
        [self.tableView reloadData];
    }else{
        _mapVC.rangeModel = _rangeModel;
        [self.navigationController pushViewController:_mapVC animated:YES];
        self.navigationController.navigationBarHidden = NO;
    }
    
}
- (void)configTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //self.tableView.tableHeaderView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"MYXLuckyTxteViewCell" bundle:nil] forCellReuseIdentifier:MYXLuckyTextReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:@"MYXSelectPhoteAndVideoTableViewCell" bundle:nil] forCellReuseIdentifier:MYXSelectPhoteCellReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:@"MXYLukyMoneySectionView" bundle:nil] forHeaderFooterViewReuseIdentifier:MYXSelectPhoteCellReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:@"MYXLuckyCategoryCell" bundle:nil] forCellReuseIdentifier:MYXLuckyCategoryCellReuseId];
    // [self.tableView registerNib:[UINib nibWithNibName:@"PostLuckyMoneyNoticeHeaderSectionView" bundle:nil] forCellReuseIdentifier:PostLuckNoticeHeaderSectionViewReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:@"PostLuckyMoneyNoticeHeaderSectionView" bundle:nil] forHeaderFooterViewReuseIdentifier:PostLuckNoticeHeaderSectionViewReuseId];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _contentViewHeight = 200;
    
}
- (void)revieveLocationChange:(NSNotification *)noti{
    LocationRangeModel *rangeModel = (LocationRangeModel *)noti.object;
    self.rangeModel = rangeModel;
    ComposeMoneyModel *model = _compViewModel.modelArray[2];
    model.sectionCateTitle = rangeModel.locationRange;
}

#pragma mark - myxLukyTextViewDelegate
- (void)addNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveNoti:) name:PostLukyImgNotificationName object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(revieveLocationChange:) name:PostChangeLocationRangeName object:nil];
}
- (void)recieveNoti:(NSNotification *)noti{
    NSMutableArray * imgArray = (NSMutableArray *)noti.object;
    NSInteger Count = imgArray.count + 2;
    NSInteger line = Count/3;
    NSLog(@"line is %ld",line);
    if (_textCell.imgArray.count == 1) {
        UIImage *image = _textCell.imgArray[0];
        CGFloat heightCons = 110;
        CGFloat withCons = 175;
        CGFloat imageHeight = image.size.height;
        CGFloat imageWidth = image.size.width;
        if (imageWidth <  imageHeight) {
            heightCons = withCons/(imageWidth/imageHeight);
            
        }else{
            withCons = ScreenWidth - 20;
            heightCons = withCons/(imageWidth/imageHeight);
            
        }
        _contentViewHeight  = PostLukyOriginalY + heightCons + PostLukylineSpace*2;
    }else{
        _contentViewHeight = PostLukyOriginalY + (PostLukylineSpace + PostLukyItemWith)*line + PostLukylineSpace;
    }
    textContent = _textCell.growTextView.text;
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:0];
    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
    _textCell.growTextView.text = textContent;
    if (_textCell.growTextView.text.length > 0) {
        _textCell.placeholderLabel.hidden = YES;
    }
    _textCell.imgArray = imgArray;
    
    [_textCell addImgToImageView];
    
}

#pragma mark - UIScrollviewdelegater
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if ([_textCell.growTextView isFirstResponder]) {
        [_textCell.growTextView resignFirstResponder];
    }
}
@end

